//
//  DRPopupView.m
//  DoctorRen
//
//  Created by Macx on 2019/1/23.
//

#import "DRPopupView.h"
#import <Masonry/Masonry.h>

@interface DRPopupView()

@property (nonatomic, strong) UIView *contentView;

@property (nonatomic, strong) UIButton *fristButton;

@property (nonatomic, strong) UIButton *secondButton;

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) UILabel *subTitleLabel;

@property (nonatomic, copy) void (^action1)(void) ;

@property (nonatomic, copy) void (^action2)(void) ;

@end

@implementation DRPopupView

-(instancetype)init{
    if (self = [super init]) {
        [self animationWithView];
    }
    return self;
}

-(instancetype)initWithTitle:(NSString *)title subTitle:(NSString *)subTitle{
    if (self = [super init]) {
        self.titleLabel.text = title;
        self.subTitleLabel.text = subTitle;
        [self animationWithView];
    }
    return self;
}

-(void) animationWithView{
    //    CATransition *fadeAnimation = [CATransition animation];
    //    fadeAnimation.duration = 0.3;
    //    // kCATransitionFade                   //交叉淡化过渡
    //    //kCATransitionMoveIn               //移动覆盖原图
    //    // kCATransitionPush                    //新视图将旧视图推出去
    //    //kCATransitionReveal                //底部显出来
    //    fadeAnimation.type = kCATransitionFade;
    //    [self.layer addAnimation: fadeAnimation forKey: nil];
    
    self.backgroundColor = [UIColor colorWithWhite:0 alpha: 0.3];
    
    [self addSubview:self.contentView];
    
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(260);
        make.center.mas_equalTo(0);
        make.width.mas_equalTo(270);
    }];
    
    [self.contentView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(40);
        make.left.mas_equalTo(40);
        make.right.mas_equalTo(-40);
    }];
    
    [self.contentView addSubview:self.subTitleLabel];
    [self.subTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLabel.mas_bottom).mas_offset(10);
        make.left.right.mas_equalTo(self.titleLabel);
    }];
    
    [self.contentView addSubview:self.fristButton];
    [self.fristButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.subTitleLabel.mas_bottom).mas_offset(10);
        make.left.right.mas_equalTo(self.titleLabel);
        make.height.mas_equalTo(44);
    }];
    
    [self.contentView addSubview:self.secondButton];
    [self.secondButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.fristButton.mas_bottom).mas_offset(10);
        make.left.right.mas_equalTo(self.fristButton);
        make.height.mas_equalTo(44);
    }];
    
    [self.fristButton addTarget:self action:@selector(fristButtonAction) forControlEvents:(UIControlEventTouchUpInside)];
    [self.secondButton addTarget:self action:@selector(secondButtonAction) forControlEvents:UIControlEventTouchUpInside];
    
    CATransition *animation = [CATransition animation];
    animation.duration = 0.2;
    animation.type = kCATransitionFade;
//    animation.subtype = kCATransitionFromTop;
    [self.contentView.layer addAnimation:animation forKey:nil];
    [self.fristButton setTitle:@"好的，现在填写" forState:(UIControlStateNormal)];
    [self.secondButton setTitle:@"返回首页" forState:(UIControlStateNormal)];
}

-(void) fristButtonAction{
    if (self.action1) {
        self.action1();
    }
    [self hiddenView:^{}];
}

-(void) secondButtonAction {
    if (self.action2) {
        self.action2();
    }
    [self hiddenView:^{}];
}

-(void)fristAction:(void (^)(void))fristAction secondAction:(void (^)(void))secondAction{
    self.action1 = fristAction;
    self.action2 = secondAction;
}

-(void) hiddenView:(void (^)(void))completeBlock{
    
    [UIView animateWithDuration:0.2 animations:^{
        self.backgroundColor = [UIColor colorWithWhite: 0 alpha: 0];
        
    } completion:^(BOOL finished) {
        if (completeBlock) {
            completeBlock();
        }
    }];
    
}

-(UIView *)contentView{
    if (!_contentView) {
        _contentView = [[UIView alloc] init];
        _contentView.backgroundColor = [UIColor whiteColor];
        _contentView.layer.cornerRadius = 7;
        _contentView.userInteractionEnabled = YES;
    }
    return _contentView;
}

-(UIButton *)fristButton{
    if (!_fristButton) {
        _fristButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _fristButton.backgroundColor = There_Green;
        [_fristButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        _fristButton.layer.cornerRadius = 5;
    }
    return _fristButton;
}

-(UIButton *)secondButton{
    if (!_secondButton) {
        _secondButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_secondButton setTitleColor:There_Green forState:(UIControlStateNormal)];
    }
    return _secondButton;
}

-(UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:20];
        _titleLabel.numberOfLines = 2;
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}

-(UILabel *)subTitleLabel {
    if (!_subTitleLabel) {
        _subTitleLabel = [[UILabel alloc] init];
        _subTitleLabel.font = [UIFont systemFontOfSize:13];
        _subTitleLabel.textColor = Text_Gray;
        _subTitleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _subTitleLabel;
}

@end
